package jml.config.cmd;

public class StringParser
{
	public String line;
	public String begin,end;
	
	public StringParser(String ln,String b, String e)
	{
		line = ln;
		begin=b;
		end=e;
	}
	
	public String parse(StringParseListener lst)
	{
		String newLine="";
		
		int i=0,j=0;
		String aux = line.substring(i,line.length());
		
		while(i>=0 && !aux.isEmpty())
		{
			i=aux.indexOf(begin);
			
			if( i>=0 )
			{
				newLine+=aux.substring(0,i);
				
				i+=begin.length();
				j=aux.indexOf(end,i);
				if( j>=0 )
				{
					String tk = aux.substring(i,j); 
					String newTk = lst.changeToken(tk);
					newLine+=newTk;
					
					i=j+end.length();
				}
				
				aux = aux.substring(i,aux.length());
			}
		}
		
		newLine+=aux;
		return newLine;
	}
	
	public static void main(String[] args)
	{
		String s = "[Pablo]";
		s = "[$Pablo] es [$Campeon] y se la RE [$BANCA], si";
//		s = "[$TITLEPATH]/[$JMLFILE]";
//		s = "[$TITLEPATH]/[$folder]";
		StringParser p = new StringParser(s,"[$","]");
		String n = p.parse(new X());
		System.out.println(n);
	}
	
	static class X implements StringParseListener
	{
		int i=0;
		@Override
		public String changeToken(String oldToken)
		{
			return "XX"+oldToken+"XX";
		}
	}
}
